package pers.qianyu.module.image.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import pers.qianyu.module.core.domain.comment.entity.CommentPO;
import pers.qianyu.module.core.domain.image.dto.NoticeDTO;
import pers.qianyu.module.core.domain.image.enums.NoticeTypeEnum;
import pers.qianyu.module.core.domain.image.vo.ImageVO;
import pers.qianyu.module.core.domain.system.vo.SysUserVO;
import pers.qianyu.module.image.listener.events.InsertCommentEvent;
import pers.qianyu.module.image.service.ImageService;
import pers.qianyu.module.image.service.NoticeService;
import pers.qianyu.module.system.service.SysUserService;

/**
 * @author mizzle rain
 * @date 2021-03-18 17:57
 */
@Component
public class InsertCommentEventListener implements ApplicationListener<InsertCommentEvent> {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private ImageService imageService;

    @Override
    public void onApplicationEvent(InsertCommentEvent insertCommentEvent) {
        CommentPO commentPO = insertCommentEvent.getCommentPO();
        NoticeDTO noticeDTO = new NoticeDTO();
        noticeDTO.setCreatedBy(commentPO.getCreatedBy());
        noticeDTO.setType(NoticeTypeEnum.COMMENT.getCode());
        noticeDTO.setContent(commentPO.getContent());

        SysUserVO sysUserVO = sysUserService.queryById(commentPO.getCreatedBy());
        noticeDTO.setCreatorName(sysUserVO.getNickname());
        noticeDTO.setCreatorAvatar(sysUserVO.getAvatar());

        ImageVO imageVO = imageService.queryById(commentPO.getTopicId());
        noticeDTO.setTargetId(imageVO.getId());
        noticeDTO.setTargetName(imageVO.getTitle());
        noticeDTO.setOwnerId(imageVO.getCreatedBy());

        sysUserVO = sysUserService.queryById(imageVO.getCreatedBy());
        noticeDTO.setOwnerName(sysUserVO.getNickname());

        noticeService.insert(noticeDTO);
    }
}
